// 练习1：请将下面的代码用ES6的写法进行改写

// 接口:
//   学生选修课选择情况表：https://www.easy-mock.com/mock/5af58399b758743d3788f8bb/courses_selection/
//   选修课表：https://www.easy-mock.com/mock/5af58399b758743d3788f8bb/courses/
'use strict';

;
(function () {
    var API = {
        COURSES_SELECTION: 'https://www.easy-mock.com/mock/5af58399b758743d3788f8bb/courses_selection/',
        COURSES: 'https://www.easy-mock.com/mock/5af58399b758743d3788f8bb/courses/'
    };

    var app = {};

    /**
     *  [学生选修课选择情况表数据]
     *  @param {Function} successCb [请求成功后的回调函数]
     *  @param {Function} failCb [请求失败后的回调函数]
     */
    app.getCoursesSelection = function (successCb, failCb) {
        $.ajax({
            url: API.COURSES_SELECTION,
            type: 'GET',
            success: function (res) {
                if (typeof successCb === 'function') {
                    successCb(res);
                }
            },
            error: function (err) {
                if (typeof failCb === 'function') {
                    failCb(err);
                }
            }
        })
    }

    /**
     *  [获取选修课表数据]
     *   @param {Function} successCb [请求成功后的回调函数]
     *   @param {Function} failCb [请求失败后的回调函数]
     */
    app.getCourses = function (successCb, failCb) {
        $.ajax({
            url: API.COURSES,
            type: 'GET',
            success: function (res) {
                if (typeof successCb === 'function') {
                    successCb(res);
                }
            },
            error: function (err) {
                if (typeof failCb === 'function') {
                    failCb(err);
                }
            }
        })
    }

    /**
     *  [创建HTML模板]
     *  @param {Array} data [数据]
     *  @return {String} html [HTML模板字符串]
     */
    app.createTemplate = function (data) {
        var html = '';
        if (data instanceof Array) {
            html += '<table>';
            html += '   <thead>';
            html += '       <tr>';
            html += '           <th>学号</th>';
            html += '           <th>姓名</th>';
            html += '           <th>年龄</th>';
            html += '           <th>所选课程</th>';
            html += '       </tr>';
            html += '   </thead>';
            html += '   <tbody>';

            data.map(function (item) {
                html += '   <tr>';
                html += '       <td>' + item.number + '</td>';
                html += '       <td>' + item.name + '</td>';
                html += '       <td>' + item.age + '</td>';
                html += '       <td>' + item.subject + '</td>';
                html += '   </tr>';
            })

            html += '   </tbody>';
            html += '</table>';
        }

        return html;
    }

    /**
     *  [初始化类]
     */
    app.initClass = function () {
        /**
         *  [构造函数Person]
         *  @param {String} name [姓名]
         *  @param {Integer} name [年龄]
         */
        var Person = function (name, age) {
            this.name = name;
            this.age = age;
        }

        Person.prototype = {
            constructor: Person,
            getName: function () {
                return this.name;
            },
            getAge: function () {
                return this.age;
            }
        }

         /**
         *  [构造函数Student]
         *  @param {String} name [姓名]
         *  @param {Integer} name [年龄]
         *  @param {String} number [学号]
         *  @param {String} subject [所选课程]
         */
        var Student = function (name, age, number, subject) {
            Person.call(this, name, age);
            this.number = number;
            this.subject = subject;
        }

        Student.prototype = new Person();
        Student.prototype.constructor = Student;
        Student.prototype.getNumber = function () {
            return this.number;
        }
        Student.prototype.getSubject = function () {
            return this.subject;
        }

        this.Student = Student;
    }

    /**
     *  [数据处理]
     */
    app.dataHandler = function () {
        var _this = this;
        this.getCoursesSelection(function (res) {
            var data1 = [].concat(res.data);
            var result = [];

            _this.getCourses(function (res) {
                data1.map(function (item1) {
                    res.data.map(function (item2) {
                        if (item1.subjectId === item2.number) {
                            item1.subjectName = item2.name;
                        }
                    })

                    return item1;
                }).map(function (item1) {
                    result.push(new _this.Student(item1.name, item1.age, item1.number, item1.subjectName));
                })

                $('#result').html(_this.createTemplate(result));
            })
        })
    }

    /**
     *  [应用初始化]
     */
    app.init = function () {
        this.initClass();
        this.dataHandler();
    }

    app.init();
})();